package com.pj.gis;

import org.geotools.data.DataUtilities;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.FeatureCollection;
import org.geotools.geojson.feature.FeatureJSON;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.opengis.geometry.Geometry;

import java.io.FileReader;
/**
 * @Author 耿佳豪
 * @Description TODO
 * @Date 2024/12/2 上午10:34
 * @Version 1.0
 */

public class GeoToolsExample {
    public static void main2(String[] args) {
        try {
            // 指定 Shapefile 文件路径
            File file = new File("C:\\Users\\耿佳豪\\Desktop\\ne_10m_admin_0_countries\\ne_10m_admin_0_countries.shp");
            URL fileURL = file.toURI().toURL();

            // 创建 ShapefileDataStore
            ShapefileDataStore dataStore = new ShapefileDataStore(fileURL);

            // 获取 FeatureType
            SimpleFeatureType featureType = dataStore.getSchema();

            // 获取 FeatureCollection
            SimpleFeatureCollection featureCollection = dataStore.getFeatureSource().getFeatures();

            // 遍历 FeatureCollection 中的每个 Feature
            try (SimpleFeatureIterator iterator = featureCollection.features()) {
                while (iterator.hasNext()) {
                    SimpleFeature feature = iterator.next();
                    System.out.println(feature.getDefaultGeometry());
                }
            }

            // 关闭 DataStore
            dataStore.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        try {
            // 指定 GeoJSON 文件路径
            File file = new File("C:\\Users\\耿佳豪\\Desktop\\geojson.json");

            // 创建 FeatureJSON 对象
            FeatureJSON fjson = new FeatureJSON();

            // 读取 GeoJSON 文件
            FeatureCollection featureCollection = fjson.readFeatureCollection(new FileReader(file));

            // 将 FeatureCollection 转换为 SimpleFeatureCollection
            SimpleFeatureCollection simpleFeatureCollection = DataUtilities.collection(featureCollection);

            // 获取 FeatureType
            SimpleFeatureType featureType = simpleFeatureCollection.getSchema();

            // 遍历 FeatureCollection 中的每个 Feature
            try (SimpleFeatureIterator iterator = simpleFeatureCollection.features()) {
                while (iterator.hasNext()) {
                    SimpleFeature feature = iterator.next();
                    // 直接使用 org.locationtech.jts.geom.Geometry 类型
                    org.locationtech.jts.geom.Geometry geometry = (org.locationtech.jts.geom.Geometry) feature.getDefaultGeometry();
                    System.out.println(geometry);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}